package com.inke.box.net;


import com.blankj.utilcode.util.NetworkUtils;
import com.inke.box.app.InKeBoxApplication;
import com.inke.box.login.vo.InKe_User;
import com.tsy.sdk.myokhttp.MyOkHttp;
import com.tsy.sdk.myokhttp.response.GsonResponseHandler;

import java.util.HashMap;

/**
 * Created by Administrator on 2017/11/1.
 */

public class InKeHttp {


    private static InKeHttp inKeHttp;
    private static String url = "";
    private MyOkHttp myOkHttp;
    private InKeHttp(){
            myOkHttp = InKeBoxApplication.getMyOkHttp();
    }

    public static InKeHttp getInstence(){
        if (inKeHttp==null) {
            inKeHttp = new InKeHttp();
        }
        return inKeHttp;
    }

    /**
     * 判断网络是否可用 ping 的ip 是百度地址 并非服务器地址
     * */
    public boolean isNetWorkSvailable(){
        if (NetworkUtils.isAvailableByPing("61.135.185.31")){
            return true;
        }else {
            return false;
        }
    }

    /**
     *登录请求
     * 请求方式   post
     * tag 网络请求标识 用于取消关闭请求
     * */
    public MyOkHttp loginHttpRequest(HashMap hashMap, final HttpCallBack httpCallBack, String tag){
        if (isNetWorkSvailable()){
            if (myOkHttp!=null)
            myOkHttp.post().url(url+HttpUrlConstant.login).tag(tag).enqueue(new GsonResponseHandler<InKe_User>() {
                @Override
                public void onFailure(int statusCode, String error_msg) {
                    httpCallBack.onFailure(statusCode,error_msg);
                }

                @Override
                public void onSuccess(int statusCode, InKe_User response) {
                    httpCallBack.onSucceed(statusCode,response);
                }
            });
        }else {
            httpCallBack.onNetWorkAvailable();
        }

        return myOkHttp;
    }


}
